package com.zhs.gccs.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.annimon.stream.Stream;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hjq.shape.view.ShapeButton;
import com.zhs.gccs.R;
import com.zhs.gccs.adapter.GccsReportGridAdapter;
import com.zhs.gccs.model.GccsOfProject;
import com.zhs.gccs.model.GccsProblem;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.ScreenUtils;
import com.zhs.gccs.vo.GccsStatistics;

import java.util.List;

/**
 * Created by chengzf on 2024/7/22.
 */
public class GccsStatisticsDialog {
    public interface OnSelectedListener {
        void onSelected(List<String> gccs);
    }

    public static class Builder extends BaseDialog.Builder<Builder> {
        List<GccsOfProject> list;
        private ShapeButton btn_reset;
        private ShapeButton btn_confirm;
        private GccsStatistics gccsStatistics;
        private RecyclerView tab_recycle_view;

        public Builder(Context context, GccsStatistics gccsStatistics, List<String> gccs,OnSelectedListener listener) {
            super(context);
            this.gccsStatistics = gccsStatistics;
            setContentView(R.layout.dialog_gccs_statistics);
            setAnimStyle(BaseDialog.ANIM_IOS);
            setGravity(Gravity.BOTTOM);
            setBackgroundDimEnabled(true);
            setCancelable(true);
            setCanceledOnTouchOutside(true);
            setWidth((int) (ScreenUtils.dpToPx(context, ScreenUtils.getScreenWidthDp(context)) * 1));
            setHeight((int) (ScreenUtils.getScreenHeightPx(context) * 0.6));
            tab_recycle_view = findViewById(R.id.tab_recycle_view);
            GccsReportGridAdapter mGridAdapter = new GccsReportGridAdapter();
            tab_recycle_view.setLayoutManager(new GridLayoutManager(context, 2));
            tab_recycle_view.setAdapter(mGridAdapter);
            if (gccsStatistics.getXmGccs() != null) {
                list = Stream.of(gccsStatistics.getXmGccs()).
                        filter(i -> !"84FC98D87113C32AE050007F0100549D".equals(i.getDbid())).toList();
                Stream.of(list).forEach(item -> {
                    String found = Stream.of(gccs).
                            filter(i -> i.equals(item.getDbid())).findFirstOrElse(null);
                    if(found!=null){
                        item.setSelected(true);
                    }else {
                        item.setSelected(false);
                    }
                });
                mGridAdapter.setNewData(list);
            }
            mGridAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    GccsOfProject gccsOfProject = list.get(position);
//                    if(!gccsOfProject.isSelected()){
//                        Stream.of(list).forEach(item->item.setSelected(false));
//                        gccsOfProject.setSelected(true);
//                    }else {
//                        gccsOfProject.setSelected(false);
//                    }
                    gccsOfProject.setSelected(!gccsOfProject.isSelected());
                    mGridAdapter.notifyDataSetChanged();
                    if (listener != null) {
                        List<String> gccss = Stream.of(list).filter(i -> i.isSelected()).map(i -> i.getDbid()).toList();
                        listener.onSelected(gccss);
                    }
                }
            });
//            btn_reset = findViewById(R.id.btn_reset);
//            btn_reset.setOnClickListener(new OnNoDoubleClickListener() {
//                @Override
//                protected void onNoDoubleClick(View v) {
//                    Stream.of(list).forEach(item -> item.setSelected(false));
//                    dismiss();
//                    if (listener != null) {
//                        listener.onSelected("");
//                    }
//                }
//            });
//            btn_confirm = findViewById(R.id.btn_confirm);
//            btn_confirm.setOnClickListener(new OnNoDoubleClickListener() {
//                @Override
//                protected void onNoDoubleClick(View v) {
//                    dismiss();
//                    String gccs = Stream.of(list).filter(GccsOfProject::isSelected).
//                            map(GccsOfProject::getDbid).findFirstOrElse(null);
//                    if (listener != null) {
//                        listener.onSelected(gccs);
//                    }
//                }
//            });
        }

    }
}
